package com.huage_08.lesson_05;

import java.io.*;
import java.net.Socket;

public class ServerThread implements Runnable {
    private Socket s;

    public ServerThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {

        try {
            //接收上传文件
            BufferedReader brServer = new BufferedReader(new InputStreamReader(s.getInputStream()));

            int count=0;
            File file = new File("class1\\src\\com\\huage_08\\lesson_05\\Copy("+count+").txt");
            while (file.exists()){
                count++;
                file = new File("class1\\src\\com\\huage_08\\lesson_05\\Copy("+count+").txt");
            }
            BufferedWriter bwServer = new BufferedWriter(new FileWriter(file));

            String line;
            while ((line= brServer.readLine())!=null){
                bwServer.write(line);
                bwServer.newLine();
                bwServer.flush();
            }

            //给予客户端回应
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw.write("上传文件成功!");
            bw.close();

            bwServer.close();
            brServer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
